﻿using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Threading;
using SGRapidForm.Common;
using System.Web;
namespace SGRapidForm.Extensions
{
    public class ContextSendInspector : IClientMessageInspector
    {
        #region IClientMessageInspector Members

        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
        }

        //2，发送请求前,检测Username是否赋值，无则赋值
        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            //获取用户名,检查实例信息的用户名为空，如果空，则从线程中获取用户信息
            //if (string.IsNullOrEmpty(ApplicationContext.Current.Username))
            //{
            //    ApplicationContext.Current.Username = Thread.CurrentPrincipal.Identity.Name;
            //}
            request.Headers.Add(new MessageHeader<ApplicationContext>(
                ApplicationContext.Current).GetUntypedHeader(
                ApplicationContext.ContextHeaderLocalName, ApplicationContext.ContextHeaderNamespace));

            return null;
        }

        #endregion
    }
}